﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using TerrariaSvcCtrl;
using System.Threading;

namespace TerrariaSVR {
    public partial class TerrariaWrapper : ServiceBase {
        Worker workerObject;
        TerrariaSvcCtrl.WorkerArgs wArgs;
        Thread workerThread;

        public TerrariaWrapper() {
            InitializeComponent();
        }

        protected override void OnStart(string[] args) {
            Console.WriteLine("Starting up! Give it a bit.", DateTime.Now);
            wArgs = new TerrariaSvcCtrl.WorkerArgs();            
            workerObject = new Worker();
            workerThread = new Thread(workerObject.StartService);

            workerObject.onPlayerJoin += new Worker.PlayerJoin(workerObject_onPlayerJoin);
            workerObject.onServerMessage += new Worker.ServerMessage(workerObject_onServerMessage);
            workerObject.onPlayerLeft += new Worker.PlayerLeft(workerObject_onPlayerLeft);

            workerThread.Start(wArgs);
        }

        void workerObject_onPlayerLeft(string username) {
            //throw new NotImplementedException();
        }

        void workerObject_onServerMessage(string message) {
            //throw new NotImplementedException();
        }

        void workerObject_onPlayerJoin(string username) {
            //throw new NotImplementedException();
            workerObject.SendChat(string.Format("Everyone welcome {0} to the game!", username));
        }

        protected override void OnStop() {
            workerObject.SaveWorld();
            workerObject.SendCommand("say SHUTING DOWN! Come back again!");
        }
    }
}
